import java.util.LinkedList;
import java.util.Scanner;

public class Manager extends Users implements LogIn{
    public Manager(){}
    public Manager(String ID,String name,String password,String registrationDate,String role,String phoneNumber){
        super(ID,name,password,registrationDate,role,phoneNumber);
    }

    @Override
    public boolean logIn(String name, String password) {
        // 从经理列表中查找匹配的用户名和密码
        CinemaSystem cinemaSystem = CinemaSystem.getInstance();
        for (Manager manager : cinemaSystem.getManagerList()) {
            if (manager.getName().equals(name) && manager.getPassword().equals(password)) {
                return true;
            }
        }
        return false;
    }
    //修改经理自己的密码
    @Override
    public void changePassword(String newPassword) {
        Scanner scanner = new Scanner(System.in);

        // 1. 获取CinemaSystem实例和经理链表
        CinemaSystem cinemaSystem = CinemaSystem.getInstance();
        LinkedList<Manager> managers = cinemaSystem.getManagerList();

        // 2. 输入当前经理姓名
        System.out.println("请输入您的用户名：");
        String currentName = scanner.nextLine();

        // 3. 查找当前经理
        Manager currentManager = null;
        for (Manager manager : managers) {
            if (manager.getName().equals(currentName)) {
                currentManager = manager;
                break;
            }
        }

        if (currentManager == null) {
            System.out.println("错误：未找到该经理账户！");
            return;
        }

        // 4. 验证当前密码
        System.out.println("请输入当前密码：");
        String currentPassword = scanner.nextLine();

        if (!currentPassword.equals(currentManager.getPassword())) {
            System.out.println("错误：当前密码不正确！");
            return;
        }

        // 5. 验证新密码
        if (newPassword == null || newPassword.trim().isEmpty()) {
            System.out.println("错误：新密码不能为空！");
            return;
        }

        if (newPassword.equals(currentManager.getPassword())) {
            System.out.println("错误：新密码不能与旧密码相同！");
            return;
        }

        // 6. 确认新密码
        System.out.println("请再次输入新密码：");
        String confirmPassword = scanner.nextLine();

        if (!newPassword.equals(confirmPassword)) {
            System.out.println("错误：两次输入的新密码不一致！");
            return;
        }

        // 7. 修改密码
        currentManager.setPassword(newPassword);
        System.out.println("密码修改成功！");
    }

    //重置前台密码为123456
    @Override
    public void resetPassword() {
        Scanner scanner = new Scanner(System.in);
        CinemaSystem cinemaSystem = CinemaSystem.getInstance();

        // 只能重置前台密码
        System.out.println("请输入要重置密码的前台用户名：");
        String targetName = scanner.nextLine();

        // 获取前台链表
        LinkedList<FrontDeskReception> frontDesks = cinemaSystem.getFrontDeskReceptionList();

        // 查找目标前台
        FrontDeskReception targetFrontDesk = null;
        for (FrontDeskReception frontDesk : frontDesks) {
            if (frontDesk.getName().equals(targetName)) {
                targetFrontDesk = frontDesk;
                break;
            }
        }

        if (targetFrontDesk == null) {
            System.out.println("错误：未找到该前台用户！");
            return;
        }

        // 重置密码为123456
        targetFrontDesk.setPassword("123456");

        // 调用Administrator的getOperatorInfo()记录操作日志
        Administrator admin = new Administrator();
        String[] operatorInfo = admin.getOperatorInfo();

        // 输出操作结果
        System.out.println("前台 " + targetName + " 的密码已重置为：123456");
    }

    //查看所有经理的信息
    @Override
    public void listAllUsers() {
        // 获取CinemaSystem实例和经理链表
        CinemaSystem cinemaSystem = CinemaSystem.getInstance();
        LinkedList<Manager> managers = cinemaSystem.getManagerList();

        // 输出表头
        System.out.println("\n========== 所有经理信息 ==========");
        System.out.println("ID\t姓名\t注册日期\t\t角色\t电话");

        // 遍历并输出每个经理的信息
        for (Manager manager : managers) {
            System.out.printf("%s\t%s\t%s\t%s\t%s\n",
                    manager.getID(),
                    manager.getName(),
                    manager.getRegistrationDate(),
                    manager.getRole(),
                    manager.getPhoneNumber());
        }

        System.out.println("===============================");
        System.out.println("共找到 " + managers.size() + " 位经理");
    }
    //用不到
    @Override
    public void deleteUser() {
    }
    //用不到
    @Override
    public void searchUser() {

    }
    //用不到
    @Override
    public void addUser() {

    }
    //用不到
    @Override
    public void modifyUser() {

    }
}

